package com.yy.wga.common.util;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.yy.wga.common.core.exception.BusinessException;
import com.yy.wga.common.util.common.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.entity.ContentType;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Objects;
import java.util.UUID;

/**
 * 阿里云oss上传
 *
 * @author lgw
 */
@Slf4j
public class OssUtils {
    private OssUtils() {
    }

    private final static String KEYED = "LTAI5t5d2ViCGS58bSGF4AVN";
    private final static String KEY = "DBZpl2mBuFwonq0bAyFAYvj1g8GKIn";
    /**
     * 阿里云访问路径这个是杭州的你要选择自己的
     */
    private final static String ENDPOINT = "oss-cn-shenzhen.aliyuncs.com";
    /**
     * 这里填你自己的bucket
     */
    private final static String BUCKETNAME = "xianrcj";

    /**
     * springboot封装用于接受文件的对象
     *
     * @param file 传过来的数据
     * @return URL
     */
    public static String URL(MultipartFile file) {
        String filename = file.getOriginalFilename();
        if (Objects.nonNull(filename)) {
            String suffix = filename.substring(filename.lastIndexOf("."));
            // 生成不重复id;
            String uuid = UUID.randomUUID().toString();
            String objectName = uuid + suffix;
            // 创建oss实例指定访问密钥
            OSS ossClient = new OSSClientBuilder().build(ENDPOINT, KEYED, KEY);
            try {
                PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKETNAME, objectName, file.getInputStream());
                ObjectMetadata objectMetadata = new ObjectMetadata();
                objectMetadata.setContentType(file.getContentType());
                putObjectRequest.setMetadata(objectMetadata);
                ossClient.putObject(putObjectRequest);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            // 返回的URL
            return "https://" + BUCKETNAME + "." + ENDPOINT + "/" + objectName;
        }
        throw new BusinessException("上传图片错误");
    }

    /**
     * 通过图片地址上传图片到oss
     *
     * @param imageUrl  图片地址
     * @param imageName 图片名称
     * @param fileName  上传到的文件夹名称
     * @return oss图片地址
     */
    public static String urlToUpload(String imageUrl, String imageName, String fileName) {
        try {
            InputStream in = new URL(imageUrl).openStream();
            String objectName;
            // 有图片名称用原图片名称，没有生成不重复id做名称;
            if (StringUtils.isNotBlank(imageName)) {
                // 去掉斜杆，有斜杆会报错
                objectName = imageName.replace("/", "");
            } else {
                String uuid = UUID.randomUUID().toString();
                objectName = uuid + "_" + imageName;
            }
            // 指定目标目录上传
            objectName = fileName + objectName;
            //创建oss实例指定访问密钥
            OSS ossClient = new OSSClientBuilder().build(ENDPOINT, KEYED, KEY);
            PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKETNAME, objectName, in);
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentType(String.valueOf(ContentType.IMAGE_JPEG));
            putObjectRequest.setMetadata(objectMetadata);
            ossClient.putObject(putObjectRequest);
            //返回的URL
            return "https://" + BUCKETNAME + "." + ENDPOINT + "/" + objectName;
        } catch (IOException e) {
            log.info("上传图片错误:{}", e);
        }
        return imageUrl;
    }
}